package com.lhr.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileUtil {

	public static String getFileMD5(File file) throws NoSuchAlgorithmException, IOException {
		if (file == null) {
			return null;
		}
		// 检查是否是文件类型
		if (!file.isFile()) {
			throw new IOException(file.getName() + "不是文件类型");
		}

		FileInputStream in = null;
		byte buffer[] = new byte[1024];
		int len;
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			in = new FileInputStream(file);
			while ((len = in.read(buffer, 0, 1024)) != -1) {
				digest.update(buffer, 0, len);
			}
			BigInteger bigInteger = new BigInteger(1, digest.digest());
			return bigInteger.toString(16);
		} catch (NoSuchAlgorithmException e) {
			throw e;
		} finally {
			if (in != null)
				in.close();
		}
	}

}
